The present disclosure relates to generating images of a virtual environment and, in certain examples, to systems and methods for rendering projected shadows for the virtual environment.
In general, one technique for rendering shadows for 3D objects over 2D backgrounds is to project a shadowing object's mesh onto a ground plane, and then blend a shadow color with pixels in a frame buffer. Such a technique can effectively darken the ground where the shadow falls without making the ground completely black, which would not look natural. Due to the blending, care must be taken to render shadow pixels only once or the shadow will appear to have dark spots where mesh triangles overlap. Overlapping triangles can occur with a single shadowing mesh or with multiple shadowing meshes whose shadows overlap.
A conventional method to avoid rendering shadow mesh pixels more than once involves using a stencil buffer. In general, a stencil buffer is a per-pixel buffer (e.g., with a depth of one byte per pixel) that can be used to add more control over which pixels are rendered. The stencil buffer can be used to, for example, limit an area of rendering (e.g., stenciling). For example, stencil data contained in the stencil buffer can be used as a general-purpose per-pixel mask for saving or discarding pixels. To use the stencil buffer to avoid rendering shadow mesh pixels more than once, the stencil buffer can be cleared to 0 before rendering. The stencil function can then be set to increment on write and a stencil test can be set to stencil=1.